//
//  MZY_DisplayCollectionViewCell.m
//  FullHouse
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_DisplayCollectionViewCell.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@implementation MZY_DisplayCollectionViewCell

- (void)dealloc
{
    [self.bottomView release];
    [self.pictureImageView release];
    [self.likeButton release];
    [self.addButton release];
    [self.lineTextField release];
    [self.likeCountLabel release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];

    }
    return self;
}

- (void)createView{
    self.pictureImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.pictureImageView];
    [self.pictureImageView release];
    
    self.bottomView = [[UIView alloc]init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bottomView];
    [self.bottomView release];
    
    self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.likeButton];
    [self.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.lineTextField = [[UITextField alloc]init];
    self.lineTextField.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.lineTextField];
    [self.lineTextField release];
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.addButton];
    
    self.likeCountLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.likeCountLabel];
    [self.likeCountLabel release];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.pictureImageView.frame = CGRectMake(0, 0, 190 * HORIZONTAL, 175 * HORIZONTAL);
    self.bottomView.frame = CGRectMake(0, 175 * VERTICAL, WIDTH, 40 * VERTICAL);
    self.likeButton.frame = CGRectMake(30 * HORIZONTAL, 180 * VERTICAL, 30 * HORIZONTAL, 30 * HORIZONTAL);
    self.lineTextField.frame = CGRectMake(WIDTH / 2, 175 * VERTICAL, 1, 40 * VERTICAL);
    self.addButton.frame = CGRectMake(WIDTH / 2 + 20 * HORIZONTAL, 180 * VERTICAL, 30 * HORIZONTAL, 30 * HORIZONTAL);
    self.likeCountLabel.frame = CGRectMake(WIDTH - 40 * HORIZONTAL, 185 * VERTICAL, 40 * HORIZONTAL, 20 * VERTICAL);
}

#pragma mark 喜欢"button"的点击方法
- (void)likeAction:(UIButton *)button{
    self.type = [[DataBaseHandle shareDB] colIsInTable:self.pic];
    if (self.type == NotInTable) {
        [[DataBaseHandle shareDB] insertPic:self.pic];
        [button setImage:[UIImage imageNamed:@"iconfont-xin2.png"] forState:UIControlStateNormal];
        self.type = InTable;
    }else if (self.type == InTable){
        [[DataBaseHandle shareDB] deletePic:self.pic];
        [button setImage:[UIImage imageNamed:@"iconfont-xin.png"] forState:UIControlStateNormal];
        self.type = NotInTable;
    }
}

@end
